<?php

namespace App\Exports;

use App\Models\Manufacturer;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class ManufacturersExport extends DefaultValueBinder implements FromArray, WithColumnWidths, WithCustomValueBinder
{
    use Exportable;

    public function __construct(public array $listQuery)
    {
    }
    public function array(): array
    {
        $status = $this->listQuery['status'] ?? null;
        $is_send = $this->listQuery['is_send'] ?? -3;
        $investment_meeting_id = $this->listQuery['investment_meeting_id'] ?? '';
        $category_id = $this->listQuery['category_id'] ?? '';
        $investment_people_id = $this->listQuery['investment_people_id'] ?? '';
        $title = $this->listQuery['title'] ?? '';
        $name = $this->listQuery['name'] ?? '';
        $phone = $this->listQuery['phone'] ?? '';
        $mobile = $this->listQuery['mobile'] ?? '';
        $wechat = $this->listQuery['wechat'] ?? '';
        $sign_status = $this->listQuery['sign_status'] ?? -1;
        $lists = Manufacturer::query()
            ->with(['category:id,name', 'person:id,name,nickname', 'meeting:id,title'])
            ->when($investment_meeting_id, function ($query) use ($investment_meeting_id) {
                $query->where('investment_meeting_id', $investment_meeting_id);
            })
            ->when($category_id, function ($query) use ($category_id) {
                $query->where('category_id', $category_id);
            })
            ->when($investment_people_id, function ($query) use ($investment_people_id) {
                $query->where('investment_people_id', $investment_people_id);
            })
            ->when($status >= -3, function ($query) use ($status) {
                $query->where('status', $status);
            })
            ->when($title, function ($query) use ($title) {
                $query->where('title', 'like', '%' . $title . '%');
            })
            ->when($name, function ($query) use ($name) {
                $query->where('name', 'like', '%' . $name . '%');
            })
            ->when($phone, function ($query) use ($phone) {
                $query->where('phone', 'like', '%' . $phone . '%');
            })
            ->when($mobile, function ($query) use ($mobile) {
                $query->where('mobile', 'like', '%' . $mobile . '%');
            })
            ->when($wechat, function ($query) use ($wechat) {
                $query->where('wechat', 'like', '%' . $wechat . '%');
            })
            ->when($is_send > -2, function ($query) use ($is_send) {
                $query->where('is_send', $is_send);
            })
            ->when($sign_status > -1, function ($query) use ($sign_status) {
                $query->where('sign_status', $sign_status);
            })
            ->orderByDesc('id')->get();
        $lists->transform(function ($item) {
            $item->manufacturer_count = Manufacturer::where('title', $item->title)->where('investment_meeting_id', $item->investment_meeting_id)->count();
            $item->phone && $item->phone_count = Manufacturer::where('phone', $item->phone)->where('investment_meeting_id', $item->investment_meeting_id)->count();
            $item->mobile && $item->mobile_count = Manufacturer::where('mobile', $item->mobile)->where('investment_meeting_id', $item->investment_meeting_id)->count();
            $item->wechat && $item->wechat_count = Manufacturer::where('wechat', $item->wechat)->where('investment_meeting_id', $item->investment_meeting_id)->count();
            $item->invitation_path && $item->invitation_url = asset('storage' . $item->invitation_path);
            $item->hand_card_path && $item->hand_card_url = asset('storage' . $item->hand_card_path);
            return $item;
        });
        $arr = [
            [
                '招商会名称',
                '品牌名称',
                '类目',
                '厂家联系人',
                '联系方式',
                '手机号码',
                '微信号码',
                '招商员',
                '提交时间',
                '短信状态',
                '报名状态',
                '签到状态',
            ]
        ];
        foreach ($lists as $v) {
            $arr[] = [
                $v->meeting->title,
                $v->title,
                $v->category->name,
                $v->name,
                $v->phone,
                $v->mobile,
                $v->wechat,
                $v->person ? $v->person->name . '【' . $v->person->nickname . '】' : '',
                $v->created_at,
                $v->is_send === 0 ? '未发送' : ($v->is_send === 1 ? '发送成功' : '发送失败'),
                $v->status === 0 ? '已提交待处理' : ($v->status === 1 ? '已通过' : ($v->status === -1 ? '没联系上' : '不通过')),
                $v->sign_status === 1 ? '已签到' : '未签到'
            ];
        }
        return $arr;
    }

    public function columnWidths(): array
    {
        return [
            'A' => 20,
            'B' => 20,
            'C' => 20,
            'D' => 20,
            'E' => 20,
            'F' => 20,
            'G' => 20,
            'H' => 20,
            'I' => 20,
            'J' => 20,
            'K' => 20,
        ];
    }

    public function bindValue(Cell $cell, $value)
    {
        $column = $cell->getColumn();
        if (in_array($column, ['E', 'F', 'G'])) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
